package com.programming.forum.services.impl;

import com.programming.forum.common.AppResult;
import com.programming.forum.common.ResultCode;
import com.programming.forum.dao.BoardMapper;
import com.programming.forum.exception.ApplicationException;
import com.programming.forum.model.Board;
import com.programming.forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22479
 * Date: 2023-11-07
 * Time: 16:44
 */

/**
 * 版块相关操作
 * IBoardService接口实现类
 */
@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;


    /**
     *
     * @param num 要查询的条数
     * @return
     */
    @Override
    public List<Board> selectByNum(Integer num) {
        //非空校验
        if(num <= 0 || num == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用dao查询数据库
        List<Board> boardList = boardMapper.selectByNum(num);

        return boardList;
    }

    /**
     *更新当前版块的发帖数 给版块帖子数量 + 1
     * @param id
     */
    @Override
    public void addOneArticleCountById(Long id) {
        if(id == null || id <= 0) {
            //FAILED_BOARD_ARTICLE_COUNT (1201, "更新版块帖子数量失败"),
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询对应版块
        Board board = boardMapper.selectByPrimaryKey(id);
        //没有找到相关的版块
        if(board == null) {
            //  ERROR_IS_NULL           (2001, "IS NULL.");
            log.warn(ResultCode.ERROR_IS_NULL.toString());

            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新帖子数量
        //更新数据时候 需要创建一个用来更新的类
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        //调用动态更新的sql语句 更新数据库
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //判断受影响的行数
        if(row != 1) {
            //  FAILED                  (1000, "操作失败"),
            log.warn(ResultCode.FAILED .toString() + "更新数据库异常");

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED ));
        }

    }


    /**
     * 版块帖子数量 - 1
     * @param id
     */
    @Override
    public void subOneArticleCountById(Long id) {
        if(id == null || id <= 0) {
            //FAILED_BOARD_ARTICLE_COUNT (1201, "更新版块帖子数量失败"),
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询对应版块
        Board board = boardMapper.selectByPrimaryKey(id);
        //没有找到相关的版块
        if(board == null) {
            //  ERROR_IS_NULL           (2001, "IS NULL.");
            log.warn(ResultCode.ERROR_IS_NULL.toString());

            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新帖子数量
        //更新数据时候 需要创建一个用来更新的类
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        //判断减一以后是否小于0
        if(updateBoard.getArticleCount() < 0) {
            updateBoard.setArticleCount(0);
        }
        //调用动态更新的sql语句 更新数据库
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //判断受影响的行数
        if(row != 1) {
            //  FAILED                  (1000, "操作失败"),
            log.warn(ResultCode.FAILED .toString() + "更新数据库异常");

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED ));
        }
    }

    /**
     * 根据版块Id查询版块详情
     * @param id 版块Id
     * @return Board 对象
     */
    @Override
    public Board selectById(Long id) {
        // 参数校验
        if (id == null || id <= 0) {
            //FAILED_PARAMS_VALIDATE  (1002, "参数校验失败"),
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board result = boardMapper.selectByPrimaryKey(id);
        return result;
    }
}
